package cn.alpha.intell.rbtlib.wifi.beans;

import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import cn.alpha.intell.rbtlib.utils.ReflectUtil;
import cn.alpha.intell.rbtlib.wifi.utils.WifiConnector;

/**
 * @author : jinlongfeng
 * @version : V2.1.11
 * @description: wifi 列表 item 模型
 * @time : 2016/11/30-22:50
 */

public class WifiListBean implements Comparable<WifiListBean> {

    //WiFi 名称
    private String SSID;
    // Wifi 信号
    private int level;
    //加密方式
    private WifiCipherType encrypt;

    private WIFISTAUTS mWIFISTAUTS;

    private int netID = -1;

    public static WifiListBean genFromScanResult(ScanResult scanResult) {

        WifiListBean wifiListBean = new WifiListBean();
        wifiListBean.setSSID(scanResult.SSID.replace("\"", ""));
        wifiListBean.setEncrypt(getFromCapabilities(scanResult.capabilities));
        wifiListBean.setLevel(WifiManager.calculateSignalLevel(scanResult.level, 4));
        wifiListBean.setWIFISTAUTS(WIFISTAUTS.DEFAULT);
        return wifiListBean;

    }

    public static WifiCipherType getFromCapabilities(String capabilities) {

        if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
            return WifiCipherType.WIFICIPHER_WPA;
        } else if (capabilities.contains("WEP") || capabilities.contains("wep")) {
            return WifiCipherType.WIFICIPHER_WEP;
        } else {
            return WifiCipherType.WIFICIPHER_NOPASS;
        }

    }

    public static WifiCipherType getCipherTypeFromConfig(WifiConfiguration wifiConfig) {
        if (wifiConfig == null) {
            return null;
        }
        // 反射调用 getAuthType 方法
        try {
            Object obj = ReflectUtil.invokeMethod(wifiConfig, "getAuthType", null);
            if (obj != null) {
                int KeyMgmt = (int)obj;
                if (KeyMgmt == WifiConfiguration.KeyMgmt.WPA_PSK) {
                    return WifiCipherType.WIFICIPHER_WPA;
                }
                else if (KeyMgmt == WifiConfiguration.KeyMgmt.WPA_EAP) {
                    return WifiCipherType.WIFICIPHER_WEP;
                }
                else{
                    return WifiCipherType.WIFICIPHER_NOPASS;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;

    }

    public static WifiListBean genFromNetworkInfo(NetworkInfo networkInfo) {

        WifiListBean wifiListBean = new WifiListBean();
        wifiListBean.setSSID(networkInfo.getExtraInfo().replace("\"", ""));
        wifiListBean.setEncrypt(WifiCipherType.WIFICIPHER_WPA);
        wifiListBean.setLevel(4);
        switch (networkInfo.getDetailedState()) {
            case AUTHENTICATING:
                wifiListBean.setWIFISTAUTS(WIFISTAUTS.AUTHORING);
                break;

            case OBTAINING_IPADDR:

                wifiListBean.setWIFISTAUTS(WIFISTAUTS.GETIPING);
                break;

            case CONNECTED:

                wifiListBean.setWIFISTAUTS(WIFISTAUTS.CONNECTD);
                break;

            case DISCONNECTED:
                wifiListBean.setWIFISTAUTS(WIFISTAUTS.DISCONNECTED);
                break;

            default:

                wifiListBean.setWIFISTAUTS(WIFISTAUTS.AUHTORFAIL);

        }
        return wifiListBean;
    }

    public static WifiListBean genFromWIfiInfo(WifiInfo wifiInfo) {

        WifiListBean wifiListBean = new WifiListBean();
        wifiListBean.setSSID(wifiInfo.getSSID().replace("\"", ""));
        wifiListBean.setLevel(WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 4));
        wifiListBean.setWIFISTAUTS(WIFISTAUTS.CONNECTD);
        wifiListBean.setEncrypt(WifiCipherType.WIFICIPHER_WPA);

        return wifiListBean;
    }

    public static WifiListBean genFromeWifiConfig(WifiConfiguration wifiConfiguration) {
        WifiListBean wifiListBean = new WifiListBean();
        wifiListBean.setSSID(wifiConfiguration.SSID.replace("\"", ""));
        // 过滤系统自动添加的
        if (WifiConnector.isSysAutoAdd(wifiConfiguration)) {
            wifiListBean.setWIFISTAUTS(WIFISTAUTS.DEFAULT);
        }
        else{
            wifiListBean.setWIFISTAUTS(WIFISTAUTS.SAVED);
        }
        wifiListBean.setNetID(wifiConfiguration.networkId);
        // 密钥类型
        wifiListBean.setEncrypt(getCipherTypeFromConfig(wifiConfiguration));
        //wifiListBean.setLevel(4);
        return wifiListBean;
    }

    public int getNetID() {
        return netID;
    }

    public void setNetID(int netID) {
        this.netID = netID;
    }

    /**
     * 倒序排列
     *
     * @param another
     * @return positive – 当前对象大于o1
     * zero – 当前对象等于o1
     * negative – 当前对象小于o1
     * @Override
     */
    public int compareTo(WifiListBean another) {


       /* int lev1 = another.netID - this.netID;

        //表示都是保存的
        if (lev1 == 0) {
            return another.level - this.level;
        } else {
            return lev1;
        }*/

        return another.level - this.level;

    }

    public String getSSID() {
        return SSID;
    }

    public void setSSID(String SSID) {
        this.SSID = SSID;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public WifiCipherType getEncrypt() {
        return encrypt;
    }

    public void setEncrypt(WifiCipherType encrypt) {
        this.encrypt = encrypt;
    }

    public WIFISTAUTS getWIFISTAUTS() {
        return mWIFISTAUTS;
    }

    public void setWIFISTAUTS(WIFISTAUTS WIFISTAUTS) {
        mWIFISTAUTS = WIFISTAUTS;
    }


    public enum WIFISTAUTS {
        SAVED,  //已经保存
        AUTHORING, //正在授权
        GETIPING,  //正在获取IP
        CONNECTD,  //已连接
        AUHTORFAIL, //密码错误
        DISCONNECTED, //断开连接
        DEFAULT      // 扫描到的

    }


    // 定义几种加密方式，一种是WEP，一种是WPA，还有没有密码的情况
    public enum WifiCipherType {
        WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
    }


}
